<project name="user" default="full">
  <target name="init">
    <property environment="env"/>

    <condition property="sphinx.available">
      <or>
        <available file="sphinx-build" filepath="${env.PATH}"/>
        <available file="sphinx-build.exe" filepath="${env.Path}"/>
        <available file="sphinx-build.exe" filepath="${env.PATH}"/>
      </or>
    </condition>
    <condition property="rst2pdf.available">
      <or>
        <available file="rst2pdf" filepath="${env.PATH}"/>
        <available file="rst2pdf.exe" filepath="${env.Path}"/>
        <available file="rst2pdf.exe" filepath="${env.PATH}"/>
      </or>
    </condition>
    <condition property="pdflatex.available">
      <or>
        <available file="pdflatex" filepath="${env.PATH}"/>
      </or>
    </condition>

    <condition property="build.directory" value="${build.directory}" else="${basedir}/target">
      <isset property="build.directory" />
    </condition>

    <condition property="project.version" value="${project.version}" else="2.20-SNAPSHOT">
      <isset property="project.version" />
    </condition>

    <echo message="Base directory is ${basedir}"/>
    <echo message="Build directory is ${build.directory} and project version is ${project.version}"/>
  </target>

  <target name="clean" depends="init">
    <delete includeemptydirs="true" failonerror="false">
      <fileset dir="${build.directory}" defaultexcludes="false">
        <include name="**/" />
      </fileset>
    </delete>
  </target>
  
  <target name="user" depends="init"
    description="Generate user html documentation for GeoServer">
    <mkdir dir="${build.directory}"/>
    <antcall target="sphinx">
      <param name="id" value="user" />
      <param name="build" value="html" />
    </antcall>
  </target>

  <target name="user-pdf" depends="init" if="pdflatex.available"
    description="Generate user PDF documentation for GeoServer">
    <mkdir dir="${build.directory}"/>
    <antcall target="sphinx">
      <param name="id" value="user" />
      <param name="build" value="latex" />
    </antcall>
    <replace file="${build.directory}/user/latex/GeoServerUserManual.tex"
             token="includegraphics" value="includegraphics[scale=0.5]"
    />
    <!-- run twice in a row to get TOC, and ignore errors -->
    <exec executable="pdflatex" failonerror="false" dir="${build.directory}/user/latex/">
      <arg line="-interaction batchmode GeoServerUserManual.tex"/>
    </exec>
    <exec executable="pdflatex" failonerror="false" dir="${build.directory}/user/latex/">
      <arg line="-interaction batchmode GeoServerUserManual.tex"/>
    </exec>
    
    <move
      file="${build.directory}/user/latex/GeoServerUserManual.pdf"
      tofile="${build.directory}/user/latex/manual.pdf"
    />
  </target>

  <target name="full" depends="user">
  </target>

  <target name="sphinx" if="sphinx.available">
                <echo message="Running sphinx-build -D release=${project.version} -W -b ${build} -d &quot;${build.directory}/${id}/doctrees&quot; . &quot;${build.directory}/${id}/${build}&quot;"/> 
    <exec executable="sphinx-build" failonerror="true" dir="${basedir}/${id}/source">
      <arg line="-D release=${project.version} -q -W --keep-going -b ${build} -d &quot;${build.directory}/${id}/doctrees&quot; . &quot;${build.directory}/${id}/${build}&quot;"/>
    </exec>
  </target>

</project>
